<template>
	<view>
		<!-- 购物车有商品时 -->
		<view class="cart-container" v-if="cart.length!==0">
			<!-- 收获地址 -->
			<my-address></my-address>
			<!-- 1 购物车商品列表的标题区域 -->
			<view class="cart-title">
				<!-- 左侧图标 -->
				<uni-icons type="shop" size="18"></uni-icons>
				<!-- 描述文本 -->
				<text class="cart-title-text">购物车</text>
			</view>
			<!-- 2 商品列表区域 -->
			<uni-swipe-action>
				<view v-for="(goods, i) in cart" :key="i">
					<uni-swipe-action-item :right-options="options" @click="deleteGoods($event,goods)">
						<view class="goods-item">
							<!-- 商品左侧图片区域 -->
							<view class="goods-item-left">
								<radio :checked="goods.goods_state" color="#c00000" @click="radioChange(goods)"></radio>
								<image :src="goods.goods_small_logo ||defaultPic" class="goods-pic" mode="widthFix">
								</image>
							</view>
							<!-- 商品右侧信息区域 -->
							<view class="goods-item-right">
								<!-- 商品标题 -->
								<view class="goods-name">
									{{goods.goods_name}}
								</view>
								<view class="goods-info-box">
									<!-- 商品价格 -->
									<view class="goods-price">
										￥{{goods.goods_price|tofixed}}
									</view>
									<!-- 商品数量 -->
									<uni-number-box :min="1" :value="goods.goods_count"
										@change="numChange($event,goods)">
									</uni-number-box>
								</view>

							</view>
						</view>
					</uni-swipe-action-item>
				</view>
			</uni-swipe-action>
			<!-- 自定义组件结算 -->
			<my-settle></my-settle>
		</view>

		<!-- 购物车没有商品时 -->
		<view class="empty-cart" v-else>
			<image src="@/static/cart.webp" mode=""></image>
			<text class="tip-text">空空如也</text>
		</view>
	</view>
</template>

<script>
	import badgeMix from '@/mixins/tabbar-badge.js'
	import {
		mapState,
		mapMutations
	} from 'vuex'
	export default {
		data() {
			return {
				// 当商品没图片时显示
				defaultPic: 'http://image5.suning.cn/uimg/b2c/newcatentries/0000000000-000000000715469339_1_400x400.jpg',
				// 滑动删除
				options: [{
					text: '删除',
					style: {
						backgroundColor: '#c00000',
					},
				}],
				timer: null
			};
		},
		mixins: [badgeMix],
		computed: {
			...mapState('m_cart', ['cart'])
		},
		methods: {
			...mapMutations('m_cart', ['updateGoodsState', 'updateGoodsCount', 'removeGoodsById']),
			radioChange(goods) {
				this.updateGoodsState({
					goods_id: goods.goods_id,
					goods_state: !goods.goods_state
				})
			},
			numChange(value, goods) {
				this.updateGoodsCount({
					goods_id: goods.goods_id,
					goods_count: value
				})
			},
			deleteGoods($event, goods) {
				// 使用防抖解决点击触发两次的bug
				clearTimeout(this.timer)
				this.timer = setTimeout(() => {
					this.removeGoodsById(goods.goods_id)
				}, 0)
			}
		},
		filters: {
			tofixed(num) {
				return Number(num).toFixed(2)
			}
		},

	}
</script>

<style lang="scss">
	.cart-container {
		padding-bottom: 50px;
	}

	.cart-title {
		height: 40px;
		display: flex;
		align-items: center;
		font-size: 14px;
		padding-left: 5px;
		border-bottom: 1px solid #efefef;

		.cart-title-text {
			margin-left: 10px;
		}
	}

	.goods-item {
		display: flex;
		padding: 10px 5px;
		border-bottom: 1px solid #e5e5e5;

		.goods-item-left {
			margin-right: 5px;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.goods-pic {
				width: 100px;
				height: 100px;
				display: block;
			}
		}

		.goods-item-right {
			flex: 1;
			display: flex;
			flex-direction: column;
			justify-content: space-between;

			.goods-name {
				font-size: 13px;
			}

			.goods-info-box {
				display: flex;
				align-items: center;
				justify-content: space-between;

				.goods-price {
					font-size: 16px;
					color: #c00000;
				}
			}
		}
	}

	.empty-cart {
		display: flex;
		flex-direction: column;
		align-items: center;
		padding-top: 150px;

		.empty-img {
			width: 90px;
			height: 90px;

			.tip-text {
				font-size: 12px;
				color: gray;
				margin-top: 35px;
			}
		}
	}
</style>
